clear
clear matrix
clear mata
set more off, perm
set matsize 11000
set maxvar 30000
cd /Users/zachbrown/Projects/PriceTransparency/Data/
global output /Users/zachbrown/Projects/PriceTransparency/Output


// open visit dataset
use clean/radiology_visit_3.dta, clear

keep age male visit_on_web n_procs_on_web n_procs_radio n_procs year member_key mm_start_yearmo mm_end_yearmo proc_class

// Collapse to yearly level
gen has_web = 0
replace has_web = 1 if visit_on_web==1
gen has_noweb = 0
replace has_noweb = 1 if visit_on_web==0

gen n_visits_web = has_web
gen n_visits_noweb = has_noweb

gen n_radio_web = n_procs_on_web
gen n_radio_noweb = n_procs_radio - n_procs_on_web

gen n_procs_web = 0
replace n_procs_web = n_procs if visit_on_web==1
gen n_procs_noweb = 0
replace n_procs_noweb = n_procs if visit_on_web==0


collapse (max) has_web has_noweb ///
	(sum) n_visits_web n_visits_noweb ///
	n_radio_web n_radio_noweb  n_procs_web n_procs_noweb ///
	(firstnm) age male ///
	, by(year member_key proc_class) fast


sort member_key year proc_class


// Merge on member characteristics

// Use middle of year for merge
gen ym = ym(year,6)

// Merge on membership info
gen yearmo = year*100 + 6
gen coverage_medical=1
gen primary_ins = 1
count


tempfile tmpdata
shell nice gunzip build/members_clean.dta -c > `tmpdata'
merge m:1 member_key yearmo coverage_medical primary_ins using `tmpdata' ///
		,keepusing(member_key yearmo std_product_type insurance_type tier primary_ins coverage_medical member_zip payernm_long payernm_short payer_on_web payer_id mm_start_yearmo mm_end_yearmo)
drop if _merge==1 

keep if coverage_medical==1
keep if primary_ins==1

gen month = substr(string(yearmo),5,2)
keep if month == "06"
drop month

gen year_tmp = substr(string(yearmo),1,4)
destring year_tmp, generate(year2)

replace year = year2 if year==.
drop if year>2011


drop _merge coverage_medical primary_ins

// Fill in age
gen yob = year - age
bys member_key: egen yob2 = mode(yob)
replace age = year - yob2
mdesc age
drop yob yob2 

// Fill in male
tab male
mdesc male
rename male tmp_male
bys member_key: egen male = min(tmp_male)
tab male
mdesc male
drop tmp_male

replace has_web = 0 if has_web==.
replace has_noweb = 0 if has_noweb==.
replace n_visits_web = 0 if n_visits_web==.
replace n_visits_noweb = 0 if n_visits_noweb==.
replace n_radio_web = 0 if n_radio_web==.
replace n_radio_noweb = 0 if n_radio_noweb==.
replace n_procs_web = 0 if n_procs_web==.
replace n_procs_noweb = 0 if n_procs_noweb==.


// Merge on member zip code characteristics
rename member_zip zip
merge m:1 zip using zip_info/Zip_chars.dta
drop if _merge==2
drop _merge zip_name zcta_lat zcta_long
rename zip member_zip
rename in_nh member_in_nh


// Fill in with average state characteristics 
foreach var of varlist zip_median_income zip_mean_income zip_pct_hs zip_pct_ba {
	bys state: egen st_`var' = mean(`var')
	*mdesc `var'
	replace `var' = st_`var' if `var'==.
	drop st_`var'
}
drop state


// Merge on comorbidities
if $use_sample==1 {
        merge m:1 member_key using build/charlson_by_member_key_sample.dta
        drop if _merge==2
        drop _merge
}

if inlist($use_sample,0,2) {
        merge m:1 member_key using build/charlson_by_member_key.dta
        drop if _merge==2
        drop _merge
}
drop weightch*
drop ynch*


// Fill in missing dummies
foreach var of varlist tier std_product_type payernm_short {
	replace `var' = 0 if `var'==.
}

// Reshape so that there is one observation for on web and one for not on web
gen exp = 2
expand exp
drop exp

sort member_key year
by member_key year: gen on_web = (_n-1)

gen has_visit = has_noweb if ~on_web
replace has_visit = has_web if on_web

gen num_visits = n_visits_noweb if ~on_web
replace num_visits = n_visits_web if on_web

gen num_procs = n_procs_noweb if ~on_web
replace num_procs = n_procs_web if on_web

drop n_visits_noweb n_visits_web n_procs_noweb n_procs_web has_noweb has_web


// Summ stats
count
tab on_web, miss
tab has_visit on_web, miss col
tab num_visits on_web if num_visits<=5, miss
tab num_procs on_web if num_procs<=5, miss


// Missing dummies
mdesc age_eom male charlindex

gen age_miss = (age_eom ==.)
replace age_eom =0 if age_miss

gen male_miss = (male==.)
replace male=0 if male_miss

replace charlindex = charlindex+1
replace charlindex=0 if (charlindex==.)

label var age_eom "Age"
label var male "Male"

// Set X controls
global ctr_vars = "age_eom age_miss male male_miss i.charlindex zip_mean_income zip_pct_ba i.rural_class i.year i.std_product_type i.payer_id"


gen post = (year>=2007)
gen web_post = (on_web==1 & post==1)
label var web_post "$\text{OnWeb}\textsubscript{m}\times\text{Post}\textsubscript{t}$"



//////////////////
// Probably of visit
//////////////////

reghdfe has_visit web_post $ctr_vars i.on_web, absorb(proc_class) vce(cl year proc_class) 
summ has_visit if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a1

//////////////////
// Number of visits 
//////////////////

reghdfe num_visits web_post $ctr_vars i.on_web, absorb(proc_class) vce(cl year proc_class) 
summ num_visits if e(sample), meanonly
estadd scalar ymeanexp = r(mean), replace
eststo a2



esttab a1 a2 using $output/utilization_regs_a.tex, frag cells(b(fmt(4) star) se(fmt(4) par)) ///
	keep(web_post )  stats() ///
	starlevels(`"\sym{*}"' 0.1 `"\sym{**}"' 0.05 `"\sym{***}"' 0.01, label(" \(p<@\)")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) ///
 	noobs collabels(none) booktabs gaps nonumber label  nonote replace nomtitles nodepvars 
	
esttab a1 a2 using $output/utilization_regs_b.tex, frag	cells(b(fmt(3) star)) ///
	drop(*)  stats(ymeanexp r2 N, fmt(2 3 %12.0fc) labels("Mean dep. var." "Adjusted R2" "Observations") layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}")) ///
	varwidth(20) modelwidth(12) delimiter(&) end(\\) collabels(none) nolines ///
	nonumber label  nonote replace nomtitles nodepvars noisily 
